package com.xb.cloud.merchantapi.product.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.merchantapi.product.service.ShopPromotionSkuApiService;
import com.xb.cloud.product.merchant.service.client.EndMerchantUserService;
//import com.xb.cloud.product.merchant.service.client.EndMerchantUserService;
import com.xb.cloud.product.merchant.service.client.ShopPromotionSkuService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
//import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.product.merchant.service.client.bo.ShopPromotionSkuBo;
import com.xb.cloud.product.merchant.service.client.input.bo.ShopPromotionSkuInput;
import com.xb.cloud.product.service.client.ProductService;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

@Service
public class ShopPromotionSkuApiServiceImpl implements ShopPromotionSkuApiService {

	@Resource
	private EndMerchantUserService endMerchantUserService;

	@Resource
	private ShopPromotionSkuService shopPromotionSkuService;
	
	@Resource
	private ProductService productService;

	@Override
	public RtnInfo createShopPromotionSku(ShopPromotionSkuInput shopPromotionSkuInput, Long endMerchantUserId) {
		EndMerchantUserBo endMerchantUserBo = null;
		ServiceReturnInfo returnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(returnInfo)) {
			if (returnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) returnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}

		ServiceReturnInfo rtnInfo = shopPromotionSkuService.createShopPromotionSku(shopPromotionSkuInput,
				endMerchantUserBo);
		if (ServiceReturnUtils.isSuccess(rtnInfo)) {
			return ReturnUtils.success();
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	/**
	 * 
	 * @param shopPromotionSkuList
	 * @param endMerchantUserId
	 * @return
	 * @see com.xb.cloud.merchantapi.product.service.ShopPromotionSkuApiService#createShopPromotionSkuList(java.util.List,
	 *      java.lang.Long)
	 */
	@Override
	public RtnInfo createShopPromotionSkuList(List<ShopPromotionSkuInput> shopPromotionSkuInputList,
			Long endMerchantUserId) {
		EndMerchantUserBo endMerchantUserBo = null;
		ServiceReturnInfo returnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(returnInfo)) {
			if (returnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) returnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}

		ServiceReturnInfo rtnInfo = shopPromotionSkuService.createShopPromotionSkuList(shopPromotionSkuInputList,
				endMerchantUserBo);
		if (ServiceReturnUtils.isSuccess(rtnInfo)) {
			return ReturnUtils.success();
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	/**
	 * 新增或修改统一调用
	 * 
	 * @param shopPromotionSkuInputList
	 * @param endMerchantUserId
	 * @return
	 * @see com.xb.cloud.merchantapi.product.service.ShopPromotionSkuApiService#createOrUpdateShopPromotionSkuBatch(java.util.List,
	 *      java.lang.Long)
	 */
	@Override
	public RtnInfo createOrUpdateShopPromotionSkuBatch(List<ShopPromotionSkuInput> shopPromotionSkuInputList,
			Long endMerchantUserId) {
		// 1.获得当前登录用户，即创建人或修改人
		EndMerchantUserBo endMerchantUserBo = null;
		ServiceReturnInfo returnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(returnInfo)) {
			if (returnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) returnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}

		// 2.拿到sku_id去判断进行插入操作还是更新操作
		List<ShopPromotionSkuInput> updateShopPromotionSkuInputList = new ArrayList<>();
		List<ShopPromotionSkuInput> createShopPromotionSkuInputList = new ArrayList<>();
		
		for (ShopPromotionSkuInput shopPromotionSkuInput : shopPromotionSkuInputList) {
			Long skuId = shopPromotionSkuInput.getSkuId();
			if (!StringUtils.isEmpty(skuId + "")) {
				ServiceReturnInfo serviceReturnInfo = shopPromotionSkuService.getShopPromotionSkuBySkuId(skuId);
				ShopPromotionSkuBo shopPromotionSkuBo = (ShopPromotionSkuBo) serviceReturnInfo.getData();
				if (shopPromotionSkuBo != null) {
					// 2.1.商品已经存在，执行更新操作(比较sort字段是否一致，是则不用更新；否则重新排序)
					if (shopPromotionSkuBo.getSort() != shopPromotionSkuInput.getSort()) {
						// 不相等，则更新
						updateShopPromotionSkuInputList.add(shopPromotionSkuInput);
					}
				} else {
					// 2.2 商品不存在，执行新增操作
					createShopPromotionSkuInputList.add(shopPromotionSkuInput);
					
				}
			}
		}
		// 3.统一新增或修改
		ServiceReturnInfo createReturnInfo = null;
		ServiceReturnInfo updateReturnInfo = null;
		if (createShopPromotionSkuInputList != null) {
			createReturnInfo = shopPromotionSkuService.createShopPromotionSkuList(createShopPromotionSkuInputList,
					endMerchantUserBo);
		}
		if (updateShopPromotionSkuInputList != null) {
			updateReturnInfo = shopPromotionSkuService.updateShopPromotionSkuList(updateShopPromotionSkuInputList,
					endMerchantUserBo);
		}
		if (ServiceReturnUtils.isSuccess(createReturnInfo) && ServiceReturnUtils.isSuccess(updateReturnInfo)) {
			return ReturnUtils.success();
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	/**
	 * 
	 * @param id
	 * @return
	 * @see com.xb.cloud.merchantapi.product.service.ShopPromotionSkuApiService#deleteShopPromotionSku(java.lang.String)
	 */
	@Override
	public RtnInfo deleteShopPromotionSku(String id) {
		ServiceReturnInfo serviceReturnInfo = shopPromotionSkuService.deleteShopPromotionSkuList(id);
		if (ServiceReturnUtils.isSuccess(serviceReturnInfo)) {
			return ReturnUtils.success();
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	/**
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param shopId
	 * @return
	 * @see com.xb.cloud.merchantapi.product.service.ShopPromotionSkuApiService#getShopPromotionSkuList(java.lang.String,
	 *      java.lang.String, java.lang.String)
	 */
	@Override
	public RtnInfo getShopPromotionSkuList(String pageNum, String pageSize, Long endMerchantUserId) {
		// 1.获得当前登录用户，即创建人或修改人
		EndMerchantUserBo endMerchantUserBo = null;
		ServiceReturnInfo returnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(returnInfo)) {
			if (returnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) returnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		Long shopId = endMerchantUserBo.getShopId();
		
		ShopPromotionSkuBo shopPromotionSkuBo = new ShopPromotionSkuBo();
		shopPromotionSkuBo.setStartPage(Integer.parseInt(pageNum));
		shopPromotionSkuBo.setPageSize(Integer.parseInt(pageSize));
		shopPromotionSkuBo.setShopId(shopId);
		ServiceReturnInfo serviceReturnInfo = shopPromotionSkuService.getShopPromotionSkuList(shopPromotionSkuBo);
		
		if (ServiceReturnUtils.isSuccess(serviceReturnInfo)) {
			ArrayList<ShopPromotionSkuBo> shopPromotionSkuBoList =   (ArrayList<ShopPromotionSkuBo>) serviceReturnInfo.getData();
			for(ShopPromotionSkuBo sps : shopPromotionSkuBoList) {
				sps.setSkuPicUrl(productService.getProductDefaultPicURL(sps.getProductId()));
			}
			return ReturnUtils.success(serviceReturnInfo);
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	/**   
	 *   
	 * @param shopPromotionSkuInputList
	 * @param endMerchantUserId
	 * @return   
	 * @see com.xb.cloud.merchantapi.product.service.ShopPromotionSkuApiService#handleShopPromotionSkuBatch(java.util.List, java.lang.Long)   
	 */
	@Override
	public RtnInfo handleShopPromotionSkuBatch(List<ShopPromotionSkuInput> shopPromotionSkuInputList,
			Long endMerchantUserId) {
		// 1.获得当前登录用户，即创建人或修改人
		EndMerchantUserBo endMerchantUserBo = null;
		ServiceReturnInfo returnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(returnInfo)) {
			if (returnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) returnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		// 2.根据shopId删除数据库记录，当前登录人获得shopId
		Long shopId = endMerchantUserBo.getShopId();
		ServiceReturnInfo serviceReturnInfo = shopPromotionSkuService.deleteShopPromotionSkuBatch(shopId);
		if(ServiceReturnUtils.isError(serviceReturnInfo)) {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		// 3.传入的新值存到数据库
		if(shopPromotionSkuInputList.size() == 0) {
			return ReturnUtils.success();
		}
		ServiceReturnInfo rtnInfo = shopPromotionSkuService.createShopPromotionSkuList(shopPromotionSkuInputList, endMerchantUserBo);
		if (ServiceReturnUtils.isSuccess(rtnInfo)) {
			return ReturnUtils.success();
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

}
